package com.alpha.runes.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alpha.runes.Common;
import com.alpha.runes.util.DisplayUtil;
import com.alpha.runes.util.SharedPreferenceUtils;
import com.alpha.runes.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义调整速度dialog
 */
public class RunesDialog extends Dialog implements View.OnClickListener {

    private Activity context;      // 上下文

    private OnCustomDialogItemClickListener listener;
    private boolean mIsDismissTouchOut = false;//是否允许触摸dialog外部区域取消显示dialog
    private int mPosition = 0; //Dialog 相对页面显示的位置
    private List<View> mViews = new ArrayList<>();//监听的View的集合
    private TextView speedCountTextView;
    private SeekBar seekBarView;
    private SharedPreferences preferences;

    public void setOnDialogItemClickListener(OnCustomDialogItemClickListener listener) {
        this.listener = listener;
    }

    int[] mIds;
    boolean isSpeeding;

    public RunesDialog(Activity mContext, int[] ids, boolean isSpeeding) {
        super(mContext, R.style.Custom_Dialog_Style); //dialog的样式
        this.context = mContext;
        this.mIds = ids;
        this.isSpeeding = isSpeeding;

        preferences = mContext.getPreferences(Context.MODE_PRIVATE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (0 == mPosition) {
            window.setGravity(Gravity.CENTER); // dialog默认显示的位置为居中
        } else {
            window.setGravity(mPosition);// 设置自定义的dialog位置
        }
        setContentView(R.layout.dialog_speed_setting);

        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.width = display.getWidth() * 7 / 8; // 设置dialog宽度为屏幕的4/5



        lp.width = DisplayUtil.dip2px(context, 360);




        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(mIsDismissTouchOut);
        //遍历控件id,添加点击事件，添加资源到集合
        for (int id : mIds) {
            View view = findViewById(id);
            view.setOnClickListener(this);
            mViews.add(view);
        }
        initView();
    }

    private void initView() {
        ImageView playView = findViewById(R.id.alphaspeed_play);
        if (isSpeeding) {
            playView.setImageResource(R.drawable.alphaspeed_onpause);
        } else {
            playView.setImageResource(R.drawable.alphaspeed_play);
        }
        speedCountTextView = findViewById(R.id.alpha_speed_count);

        seekBarView = findViewById(R.id.alphaspeed_seekbar);
        seekBarView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                currentSpeed = (i + 1);
                speedCountTextView.setText(String.valueOf(currentSpeed));

                SharedPreferenceUtils.putData(preferences, Common.CURRENT_SPEED_COUNT, currentSpeed);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        SharedPreferences preferences = context.getPreferences(Context.MODE_PRIVATE);
        int speedProgress = (int) SharedPreferenceUtils.getData(preferences, Common.CURRENT_SPEED_COUNT, 10);
        seekBarView.setProgress(speedProgress - 1);

        speedCountTextView.setText(String.valueOf(currentSpeed));
    }

    /**
     * 获取需要监听的View集合
     *
     * @return
     */
    public List<View> getViews() {
        return mViews;
    }

    @Override
    public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, @Nullable Menu menu, int deviceId) {

    }

    public interface OnCustomDialogItemClickListener {
        void OnCustomDialogItemClick(RunesDialog dialog, View view);
    }


    @Override
    public void onClick(View view) {
        //是否默认所有按钮点击后取消dialog显示，false是需要在点击事件后手动调用dismiss。
        listener.OnCustomDialogItemClick(this, view);
    }


    private int currentSpeed = 10;

    public int getCurrentSpeed() {
        return currentSpeed;
    }

    /**
     * 加法
     */
    public void setAddSpeed() {
        if (currentSpeed >= 10) {
            currentSpeed = 10;
        } else {
            currentSpeed += 1;
        }
        seekBarView.setProgress(currentSpeed - 1);
        speedCountTextView.setText(String.valueOf(currentSpeed));
    }

    /**
     * 减法
     */
    public void setSubSpeed() {
        if (currentSpeed <= 1) {
            currentSpeed = 1;
        } else {
            currentSpeed -= 1;
        }
        seekBarView.setProgress(currentSpeed - 1);
        speedCountTextView.setText(String.valueOf(currentSpeed));
    }
}
